//
//  JCNewFeatureController.swift
//  weibo07777
//
//  Created by mac on 15/12/17.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SnapKit

class JCNewFeatureController: UICollectionViewController {
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private let ReuseIdentifier = "ReuseIdentifier"
    
    private let ImageCount = 4
    
    private let layout = UICollectionViewFlowLayout()
    
    init(){
        super.init(collectionViewLayout: layout)
        prepareCollectionView()
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // Uncomment the following line to preserve selection between presentations
        // self.clearsSelectionOnViewWillAppear = false
        
        // Register cell classes
        self.collectionView!.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        
        // Do any additional setup after loading the view.
    }
    
    private func prepareCollectionView(){
        
        collectionView?.registerClass(JCCollectionViewCell.self, forCellWithReuseIdentifier: ReuseIdentifier)
        
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.pagingEnabled = true
        collectionView?.bounces = false
        
        
    }
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return ImageCount
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(ReuseIdentifier, forIndexPath: indexPath) as!JCCollectionViewCell
        
        cell.index = indexPath.item
        
        return cell
    }
    
    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
        
        // 通过屏幕中点判断当前IndexPath
        let pInView = view.convertPoint(collectionView.center, toView: collectionView)
        let visibleIndexPath = collectionView.indexPathForItemAtPoint(pInView)
        
        // 在collectionView停下来,我们自己来判断是否是最后一个cell
        if visibleIndexPath?.item == ImageCount - 1 {
            // 显示的是最后一个cell
            
            // 根据indexPath获取cell
            let cell = collectionView.cellForItemAtIndexPath(visibleIndexPath!) as! JCCollectionViewCell
            cell.startButtonAnimation()
        }
    }
    

    // MARK: JCCollectionViewCell
    class JCCollectionViewCell: UICollectionViewCell {
        
        func startButtonAnimation() {
        
            startButton.hidden = false
            startButton.transform = CGAffineTransformMakeScale(0, 0)
            
            UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 10, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
                // 设置X/Y缩放都为1, 清空到原始状态(X/Y缩放都为1)
                self.startButton.transform = CGAffineTransformIdentity
                },completion: nil)

        
        }
        
        /// 第几个cell
        var index: Int! {
            didSet {
                // 根据是第几个cell显示对应的图片
                bgimgView.image = UIImage(named: "new_feature_\(index + 1)")
                
                startButton.hidden = true
            }
        }

        
        override init(frame: CGRect) {
            super.init(frame: frame)
            
            prepareUI()
        }
        
        required init?(coder aDecoder: NSCoder) {
            fatalError("init(coder:) has not been implemented")
        }
        
        func prepareUI(){
            
            contentView.addSubview(bgimgView)
            contentView.addSubview(startButton)
            self.setNeedsUpdateConstraints()
        }
        
        
        
        // MARK : 约束
        override func updateConstraints() {
            
            bgimgView.snp_makeConstraints { (make) -> Void in
                make.edges.equalTo(EdgeInsetsMake(0, left: 0, bottom: 0, right: 0))
            }
            startButton.snp_makeConstraints { (make) -> Void in
                make.centerX.equalTo(contentView)
                make.bottom.equalTo(contentView).offset(-160)
            }
            
            super.updateConstraints()
        }
        
        // MARK : lazy
        private lazy var bgimgView = UIImageView(image: UIImage(named: "new_feature_1"))
        
        /// 开始按钮
        private lazy var startButton: UIButton = {
            let button = UIButton()
            
            // 设置背景
            button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
            button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
            
            // 设置文字
            button.setTitle("开始体验", forState: UIControlState.Normal)
            
            // 添加点击事件
            button.addTarget(self, action: "buttonClick", forControlEvents: UIControlEvents.TouchUpInside)
            
            return button
        }()
        
        func buttonClick(){
        
            AppDelegate.switchRootViewController(false)
            
        }
        
    }
}
